//
//  MYBaseViewController.m
//  hehe
//
//  Created by Robin on 2016/9/26.
//  Copyright © 2016年 HolyShit. All rights reserved.
//

#import "MYBaseViewController.h"

static const CGFloat NavHeight = 44.0;
static const CGFloat NavMinWH = 40.0;
static const CGFloat NavTitleFontSize = 14.0;

@interface MYBaseViewController ()

@end

@implementation MYBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WHITE_COLOR;
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
    [self showBarButton:MYNavPosLeft imageName:@"icon_return"];
}

-(void)setTitle:(NSString *)title
{
    [super setTitle:title];
    UILabel *titleLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    titleLb.font = MYFont(18);
    titleLb.textColor = WHITE_COLOR;
    titleLb.text = title;
    [titleLb sizeToFit];
    [self setTitleView:titleLb];
}

-(void)rightTitleWithTitle:(NSString *)title
{
    [self rightTitleWithTitle:title color:WHITE_COLOR];
    self.rightNavBtn.titleEdgeInsets = UIEdgeInsetsMake(5, 0, 0, 0);
}

- (void)rightTitleWithIconText:(NSString *)iconText color:(UIColor *)color
{
    [self rightTitleWithTitle:iconText color:color];
    self.rightNavBtn.titleLabel.font = IconFont(20);
    self.rightNavBtn.frame = CGRectMake(self.rightNavBtn.left - 4, self.rightNavBtn.top, self.rightNavBtn.width + 8, self.rightNavBtn.height);
}

-(void)rightTitleWithTitle:(NSString *)title color:(UIColor *)color
{
    UIButton *button = [self navigationButtonWithTitle:title color:color];
    self.rightNavBtn = button;
    [button addTarget:self action:@selector(rightButtonTouch) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -6;
    
    self.navigationItem.rightBarButtonItems = nil;
    UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:button];
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];
}

-(void)leftTitleWithTitle:(NSString *)title
{
    [self leftTitleWithTitle:title color:WHITE_COLOR];
    self.leftNavBtn.titleEdgeInsets = UIEdgeInsetsMake(5, 0, 0, 0);
}

-(void)leftTitleWithTitle:(NSString *)title color:(UIColor *)color
{
    [self showBarButton:MYNavPosLeft title:title fontColor:color];
}

- (void)leftTitleWithIconText:(NSString *)iconText color:(UIColor *)color
{
    [self leftTitleWithTitle:iconText color:color];
    self.leftNavBtn.titleLabel.font = IconFont(20);
}

- (void)showBarButton:(MYNavPos)position title:(NSString *)name fontColor:(UIColor *)color{
    UIButton *button = [self navigationButtonWithTitle:name color:color];
    [self showBarButton:position button:button];
}

- (void)showBarButton:(MYNavPos)position imageName:(NSString *)imageName{
    UIButton *button = [self navigationButton:[UIImage imageNamed:imageName]];
    [self showBarButton:position button:button];
}

- (void)showBarButton:(MYNavPos)position button:(UIButton *)button{
    if (MYNavPosLeft == position) {
        self.leftNavBtn = button;
        [button addTarget:self action:@selector(leftButtonTouch) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.leftBarButtonItem = nil;
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
        if (IOS7_OR_LATER) {
            self.navigationController.interactivePopGestureRecognizer.delegate = nil;
        }
    }
    else{
        self.rightNavBtn = button;
        [button addTarget:self action:@selector(rightButtonTouch) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = nil;
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    }
}

- (void)setTitleView:(UIView *)titleView{
    self.navigationItem.titleView = titleView;
}

- (void)leftButtonTouch{
    [self my_pop:YES];
}

- (void)rightButtonTouch{
}

-(UIButton *)navigationButton:(UIImage *)image{
    CGRect buttonFrame = CGRectZero;
    
    if(IOS7_OR_LATER){
        buttonFrame = CGRectMake(0, 0, image.size.width, NavHeight);
    }
    else{
        buttonFrame = CGRectMake(0, 0, image.size.width + 10.0f, NavHeight);
    }
    if(buttonFrame.size.width < NavMinWH){
        buttonFrame.size.width = NavMinWH;
    }
    if(buttonFrame.size.height < NavMinWH){
        buttonFrame.size.height = NavMinWH;
    }
    UIButton *btn = [[UIButton alloc] initWithFrame:buttonFrame];
    btn.contentMode = UIViewContentModeScaleAspectFit;
    btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    btn.backgroundColor = [UIColor clearColor];
    [btn setImage:image forState:UIControlStateNormal];
    return btn;
}

-(UIButton *)navigationButtonWithTitle:(NSString *)str color:(UIColor *)color{
    CGRect buttonFrame = CGRectZero;
    
    CGSize titleSize = [str boundingRectWithSize:CGSizeMake(999999.0f, NavHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:NavTitleFontSize]} context:nil].size;
    
    if (IOS7_OR_LATER){
        buttonFrame = CGRectMake(0, 0, titleSize.width, NavHeight);
    }
    else{
        buttonFrame = CGRectMake(0, 0, titleSize.width + 10.0f, NavHeight);
    }
    
    UIButton *btn = [[UIButton alloc] initWithFrame:buttonFrame];
    btn.contentMode = UIViewContentModeScaleAspectFit;
    btn.backgroundColor = [UIColor clearColor];
    btn.titleLabel.font = [UIFont systemFontOfSize:NavTitleFontSize];
    [btn setTitleColor:color forState:UIControlStateNormal];
    [btn setTitle:str forState:UIControlStateNormal];
    return btn;
}

/**
 *  导航控制器 统一管理状态栏颜色
 *  @return 状态栏颜色
 */
-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
//-------------每个vc自己控制-------------//
// 是否支持自动转屏
- (BOOL)shouldAutorotate
{
    return NO;
}
// 支持哪些屏幕方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

// 默认的屏幕方向（当前ViewController必须是通过模态出来的UIViewController（模态带导航的无效）方式展现出来的，才会调用这个方法）
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

@end
